package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainSubmitApplyBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

public class SessionApplyActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.edt_train_name)
    EditText edtTrainName;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.edt_train_code)
    EditText edtTrainCode;
    @BindView(R.id.edt_train_edu)
    EditText edtTrainEdu;
    @BindView(R.id.edt_train_school)
    EditText edtTrainSchool;
    @BindView(R.id.edt_train_jobaddres)
    EditText edtTrainJobaddres;
    @BindView(R.id.edt_train_phone)
    EditText edtTrainPhone;
    @BindView(R.id.edt_train_wechat)
    EditText edtTrainWechat;
    @BindView(R.id.edt_train_email)
    EditText edtTrainEmail;
    @BindView(R.id.practice_confirm)
    Button practiceConfirm;
    private  boolean  isSelected=false;
    private TextView maleTv;
    private TextView femaleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session_apply);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();

        if (intent!=null){
            String train = intent.getStringExtra("train");
            String courseprice = intent.getStringExtra("courseprice");
            SubmitTrainApply(token,userId,train,courseprice);
        }
        initView();
    }

    private void initView() {

        ImageView mBackIv = (ImageView) findViewById(R.id.iv_back);
        maleTv = (TextView) findViewById(R.id.male_tv);
        femaleTv = (TextView) findViewById(R.id.female_tv);
        mBackIv.setOnClickListener(this);

         maleTv.setOnClickListener(this);
         femaleTv.setOnClickListener(this);


         if (TextUtils.isEmpty(edtTrainName.getText().toString())){
             ToastUtil.showShortToast(this,"名字不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainCode.getText().toString())){
             ToastUtil.showShortToast(this,"身份证不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainEdu.getText().toString())){
             ToastUtil.showShortToast(this,"学历不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainSchool.getText().toString())){
             ToastUtil.showShortToast(this,"学校不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainPhone.getText().toString())){
             ToastUtil.showShortToast(this,"手机号不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainWechat.getText().toString())){
             ToastUtil.showShortToast(this,"微信不能为空");
             return;
         }else if (TextUtils.isEmpty(edtTrainEmail.getText().toString())){
             ToastUtil.showShortToast(this,"邮箱不能为空");
             return;
         }


    }

    private void SubmitTrainApply(String token, String userId,String train,String courseprice) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("userId",userId);
        map.put("train",train);
        map.put("courseprice",courseprice);
        map.put("name",edtTrainName);
        map.put("sex",maleTv);
        map.put("certificates",edtTrainCode);
        map.put("education",edtTrainEdu);
        map.put("school",edtTrainSchool);
        map.put("company",edtTrainJobaddres);
        map.put("telephone",edtTrainPhone);
        map.put("wechat",edtTrainWechat);
        map.put("email",edtTrainEmail);



        HttpUtil.getInstance().getApiInterface().trainSubmitApply(map).enqueue(new CallbackImple<TrainSubmitApplyBean>() {
            @Override
            public void onError(Call<TrainSubmitApplyBean> call, Throwable t) {
                ToastUtil.showShortToast(SessionApplyActivity.this,t.getMessage());

            }

            @Override
            public void onSuccess(Call<TrainSubmitApplyBean> call, TrainSubmitApplyBean response) {
                if (response.getStatus()==200){

                    if (response.getData()!=null){
                        TrainSubmitApplyBean.DataBean data = response.getData();
                        String number = data.getNumber();

                        ToastUtil.showShortToast(SessionApplyActivity.this,number);

                    }else {
                        ToastUtil.showShortToast(SessionApplyActivity.this,response.getMsg());
                    }

                }else {
                    ToastUtil.showShortToast(SessionApplyActivity.this,"数据加载失败");
                }

            }
        });





    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.male_tv:
                if (isSelected){
                    maleTv.setBackgroundResource(R.drawable.select_no_press);
                }else {
                    maleTv.setBackgroundResource(R.drawable.select_press);
                }
                break;
            case R.id.female_tv:
                if (isSelected){
                    femaleTv.setBackgroundResource(R.drawable.select_no_press);
                }else {
                    femaleTv.setBackgroundResource(R.drawable.select_press);
                }

                break;

        }
    }
}
